package org.szg.controller;

//import com.baidubce.qianfan.Qianfan;
//import com.baidubce.qianfan.model.chat.ChatResponse;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import org.springframework.web.bind.annotation.*;
import org.szg.controller.common.AliyunAI;

@CrossOrigin
@RestController
@RequestMapping("/res")
public class ResController {
//    @GetMapping("/{question}")
//    public String res(@PathVariable String question){
//        // 使用安全认证AK/SK鉴权参数，安全认证Access Key替换your_iam_ak，Secret Key替换your_iam_sk
//        Qianfan qianfan = new Qianfan("your_iam_ak" , "your_iam_sk" );
//        //指定模型
//        ChatResponse resp = qianfan.chatCompletion()
//                .model("ERNIE-4.0-8K")
//                .addMessage( "user",question)
//                .execute();
//        System.out.println(resp.getResult());
//        return resp.getResult();
//    }
    @GetMapping("/{question}")
    public String res(@PathVariable String question){
        try {
            GenerationResult result = AliyunAI.callWithMessage(question);
            System.out.println(JsonUtils.toJson(result));
            return JsonUtils.toJson(result);
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // 使用日志框架记录异常信息
            System.err.println("调用aliyun服务失败" + e.getMessage());
        }
        return "调用aliyun服务失败";
    }
}
